﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "VoxelEngine/Diffuse"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		_GridTex("背景框", 2D) = "white" {}
		_SelectTex("选择背景", 2D) = "white" {}
		_Color("Color", Color) = (1,1,1,1)
	}
		SubShader
		{
			// No culling or depth
			//Cull Off ZWrite Off ZTest Always

			//Blend SrcAlpha OneMinusSrcAlpha
			Pass
			{
				Blend SrcAlpha OneMinusSrcAlpha
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag

				#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;
				float4 color : COLOR0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;
				float4 vertex : SV_POSITION;
				float4 color : COLOR0;
			};

			v2f vert(appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				o.uv2 = v.uv2;
				o.color = v.color;
				//o.color = v.normal * 0.5 + fixed3(0.5, 0.5, 0.5);
				return o;
			}

			sampler2D _MainTex;
			sampler2D _GridTex;
			sampler2D _SelectTex;
			float4 _Color;

			fixed4 frag(v2f i) : SV_Target
			{
				if (i.color.a <= 0.1)
				{
					i.color.a = 1;
					fixed4 colorShadow = tex2D(_SelectTex, i.uv2);
					if (colorShadow.r == colorShadow.g)//白色
					{
						fixed4 col = tex2D(_MainTex, i.uv) * i.color * _Color;
						return col;
					}
					else
					{
						fixed4 col = colorShadow;
						return col;
					}
				}
				else
				{
					fixed4 col = tex2D(_MainTex, i.uv) * i.color * tex2D(_GridTex, i.uv2) * _Color;
					return col;
				}
			}
			ENDCG
		}
	}
}
